package com.dfdchem.imd.kaiwu.adapter;

import android.content.Context;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.request.RequestOptions;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.manager.GlideManager;
import com.dfdchem.imd.kaiwu.model.BannerBean;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

public class HomeBannerAdapter extends BannerAdapter<BannerBean, HomeBannerAdapter.BannerViewHolder> {
    private static final String TAG = "HomeBannerAdapter";

    private Context context;

    public HomeBannerAdapter(Context context, List<BannerBean> datas) {
        super(datas);
        this.context =context;
    }

    @Override
    public HomeBannerAdapter.BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        ImageView imageView = new ImageView(parent.getContext());
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return new BannerViewHolder(imageView);
    }

    @Override
    public void onBindView(HomeBannerAdapter.BannerViewHolder holder, BannerBean data, int position, int size) {

        holder.imageView.setImageDrawable(context.getDrawable(R.drawable.icon_loading));
        holder.imageView.setScaleType(ImageView.ScaleType.CENTER);

        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.icon_loading)//图片加载出来前，显示的图片
                .fallback(R.drawable.image_default) //url为空的时候,显示的图片
                .error(R.drawable.image_default);

        new GlideManager(context).load(holder.imageView, data.getImageUrl(), options);
    }

    @Override
    public void setDatas(List<BannerBean> datas) {
        super.setDatas(datas);
    }

    class BannerViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        public BannerViewHolder(@NonNull ImageView view) {
            super(view);
            this.imageView = view;
        }
    }
}
